package com.youlai.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.mapper.ExampleTeacherMapper;
import com.youlai.system.pojo.entity.ExampleStudent;
import com.youlai.system.pojo.entity.ExampleTeacher;
import com.youlai.system.pojo.form.StudentForm;
import com.youlai.system.pojo.form.TeacherForm;
import com.youlai.system.pojo.query.TeacherPageQuery;
import com.youlai.system.pojo.vo.StudentPageVO;
import com.youlai.system.pojo.vo.TeacherPageVO;
import com.youlai.system.service.IExampleTeacherService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhi
 * @since 2024-04-25
 */
@Service
public class ExampleTeacherServiceImpl extends ServiceImpl<ExampleTeacherMapper, ExampleTeacher> implements IExampleTeacherService {

    @Override
    public boolean deleteTeacher(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除数据为空");
        //
        List<Long> idsList = Arrays.asList(ids.split(","))
                .stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());

        // 删除字典数据项
        boolean result = this.removeByIds(idsList);

        return result;
    }

    @Override
    public boolean updateTeacher(Long id, TeacherForm teacherForm) {
        ExampleTeacher exampleTeacher = new ExampleTeacher();
        BeanUtil.copyProperties(teacherForm, exampleTeacher);

        boolean result = this.updateById(exampleTeacher);


        return result;
    }

    @Override
    public boolean saveTeacher(TeacherForm teacherForm) {
        ExampleTeacher exampleTeacher = new ExampleTeacher();
        BeanUtil.copyProperties(teacherForm, exampleTeacher);

        boolean result = this.save(exampleTeacher);


        return result;
    }

    @Override
    public TeacherForm getTeacherForm(Long id) {
        ExampleTeacher exampleTeacher = this.getOne(new LambdaQueryWrapper<ExampleTeacher>()
                .eq(ExampleTeacher::getId, id)
                .select(
                        ExampleTeacher::getId,
                        ExampleTeacher::getTeacherName,
                        ExampleTeacher::getTeacherAge,
                        ExampleTeacher::getTeacherCar,
                        ExampleTeacher::getTeacherAddress,
                        ExampleTeacher::getTeacherPhone

                )
        );
        TeacherForm teacherForm = new TeacherForm();
        BeanUtil.copyProperties(exampleTeacher, teacherForm);


        return teacherForm;
    }

    @Override
    public Page<TeacherPageVO> getTeacherPage(TeacherPageQuery queryParams) {
        //查询参数
        //查询页数
        int pageNum = queryParams.getPageNum();
        //查询数量大小
        int pageSize = queryParams.getPageSize();
        //查询条件
        String keywords = queryParams.getKeywords();

        Page<ExampleTeacher> teacherPage = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<ExampleTeacher>()
                        /**
                         * .like 模糊查询，这里就是模糊查询学生姓名
                         * .select 就是将查询出来的信息显示出来，这里就是全显示
                         */
                        .like(StrUtil.isNotBlank(keywords), ExampleTeacher::getTeacherName, keywords)
                        .select(ExampleTeacher::getId, ExampleTeacher::getTeacherName, ExampleTeacher::getTeacherAge, ExampleTeacher::getTeacherCar,ExampleTeacher::getTeacherAddress,ExampleTeacher::getTeacherPhone)
        );

        Page<TeacherPageVO> teacherPageVOPage = new Page<>();
        BeanUtil.copyProperties(teacherPage, teacherPageVOPage);


        return teacherPageVOPage;
    }
}
